package leetcode.lcp0030;

import java.util.PriorityQueue;

import leetcode.IDebug;
import leetcode.helper.H;

public class Solution implements IDebug {

	public int magicTower(int[] nums) {
//    	[100,100,100,-250,-60,-140,-50,-50,100,150]
		PriorityQueue<Integer> queue = new PriorityQueue<>((a, b) -> a - b);
		long sum = 1, tail = 0;
		int ans = 0;
		for (int i = 0; i < nums.length; i++) {
			int num = nums[i];
			if (num < 0) {
				queue.add(num);
			}
			sum += num;
			while (sum < 1 && !queue.isEmpty()) {
				int min = queue.poll();
				sum -= min;
				tail += min;
				ans += 1;
			}
		}
		sum += tail;
		return sum < 1 ? -1 : ans;
	}

	@Override
	public void debug4() {
		H.compare(1, this.magicTower(H.str2array3("[100,100,100,-250,-60,-140,-50,-50,100,150]")));
		H.compare(-1, this.magicTower(H.str2array3("[-200,-300,400,0]")));

	}

	@Override
	public void debug3() {
		// TODO Auto-generated method stub

	}

	@Override
	public void debug2() {
		// TODO Auto-generated method stub

	}

	@Override
	public void debug1() {
		// TODO Auto-generated method stub

	}

	public static void main(String[] args) {
		Solution so = new Solution();
		so.debug1();
		so.debug2();
		so.debug3();
		so.debug4();

	}

}
